<template>
  <static-content-wrapper
    :designer="designer"
    :settings="settings"
    :field="field"
    :design-state="designState"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
    :allow-select="allowSelect"
  >
    <div ref="fieldEditor" class="field-editor text-field-editor" :style="style">
      <span>{{ content }}</span>
    </div>
  </static-content-wrapper>
</template>

<script>
import emitter from '@/mixins/emitter'
import fieldMixin from '@/mixins/fieldMixin'

export default {
  name: 'TextWidget',
  componentName: 'FieldWidget',
  // 必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    settings: Object,
    scale: {
      type: Number,
      default: 1
    },
    allowSelect: {
      type: Boolean,
      default: true
    },

    designState: {
      type: Boolean,
      default: false
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: ''
    }
  },
  computed: {
    scaleWidth() {
      return this.field.options.width * this.scale
    },
    scaleHeight() {
      return this.field.options.height * this.scale
    },
    scaleBorderWidth() {
      return this.field.options.widthLine * this.scale
    },
    scaleBorderRadius() {
      return this.field.options.radius * this.scale
    },
    scaleFontSize() {
      return this.field.options.fontSize * this.scale
    },
    content() {
      return this.field.options.widgetValue || 'Text'
    },
    justifyContent() {
      const align = this.field.options.staticTextAlign
      if (align === 1) {
        return 'start'
      } else if (align === 3) {
        return 'end'
      } else {
        return 'center'
      }
    },
    style() {
      return {
        display: 'flex',
        justifyContent: this.justifyContent,
        alignItems: 'center',
        width: this.scaleWidth + 'px',
        height: this.scaleHeight + 'px',
        borderStyle: 'solid',
        borderWidth: this.scaleBorderWidth + 'px',
        borderColor: this.field.options.colorLine,
        borderRadius: this.scaleBorderRadius + 'px',
        backgroundColor: this.field.options.isCover ? this.field.options.colorLine : 'inherit',
        opacity: this.field.options.opa / 255,
        fontSize: this.scaleFontSize + 'px',
        color: this.field.options.fontColor,
        fontWeight: this.field.options.fontType === 2 ? 'bold' : 'normal',
        fontStyle: this.field.options.fontType === 1 ? 'italic' : 'normal'
      }
    }
  },
  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */
    this.registerToRefList()
    this.initEventHandler()

    this.handleOnCreated()
  },

  mounted() {
    this.handleOnMounted()
  },

  beforeUnmount() {
    this.unregisterFromRefList()
  },

  methods: {}
}
</script>

<style lang="scss" scoped></style>
